package ModelLayer;
import java.util.*;

/**
 * Write a description of class AddressBook here.
 * 
 * @author (Group2 ) 
 * @version (26)
 */
public class AddressBook
{  
   private static final AddressBook instance = new AddressBook();        //singleton
   
   public ArrayList<Person> addressb;             //array list for persons
   
   public AddressBook()     //constructor
   {
       addressb=new ArrayList();
   }

    public static AddressBook getInstance()    //singleton
    {
        return instance;
    }

    public void addPerson(Person person)        //To add an another person to the list
    {
        addressb.add(person);    
    }

    public void createPerson(String id, String name, String address, int postalCode, String city, String phone)   //To create a new person from the person class and add it to the list
    {
        Person person=new Person(id, name, address, postalCode, city, phone);
        addressb.add(person);
    }

    public void getList()    //return the list of persons with their datas
    {
        for (Person persons:addressb)
        {
            System.out.println("   " + "Name:"+ persons.getName());
            System.out.println("   " + "Address:" +persons.getaddress());
            System.out.println("   " + "Postal Code:"+ persons.getPostalCode());
            System.out.println("   " + "City:" +persons.getCity());
            System.out.println("   " + "Phone:" +persons.getphone());
            System.out.println("********************************");             
        }
    }

    public void getPerson(String id)    //return a specified person;
    {   
        String name;
        name=null;
        String address;
        address=null;
        int PostalCode;
        PostalCode=0;
        String City;
        City=null;
        String Phone;
        Phone=null;

        for(Person a: addressb)
        {
            if(a.id.equals(id))
            {
                System.out.println("   " + "Name:"+ a.getName());
                System.out.println("   " + "Address:" +a.getaddress());
                System.out.println("   " + "Postal Code:"+ a.getPostalCode());
                System.out.println("   " + "City:" +a.getCity());
                System.out.println("   " + "Phone:" +a.getphone());
                System.out.println("********************************");             
            }
        }
    }
   
    public void updatePersonName(String id,String name)    //update a specified persons name;
    {
        ArrayList<Person> select=new ArrayList<Person>();

        for(Person a: addressb)
        {
            if(a.id.equals(id))
            {
                select.add(a);
                a.setName(name);
            }   

        }

    }

    public void updatePersonAddress(String id,String address)        //update a specified persons address;
    {
        ArrayList<Person> select=new ArrayList<Person>();

        for(Person a: addressb)
        {
            if(a.id.equals(id))
            {
                select.add(a);
                a.setAddress(address);
            }   

        }

    }

    public void updatePersonPostalCode(String id,int PostalCode)        //update a specified persons postal code;
    {
        ArrayList<Person> select=new ArrayList<Person>();

        for(Person a: addressb)
        {
            if(a.id.equals(id))
            {
                select.add(a);
                a.setPostalCode(PostalCode);
            }   

        }

    }

    public void updatePersonCity(String id,String City)     //update a specified persons city;
    {
        ArrayList<Person> select=new ArrayList<Person>();

        for(Person a: addressb)
        {
            if(a.id.equals(id))
            {
                select.add(a);
                a.setCity(City);
            }   

        }

    }

    public void updatePersonPhone(String id,String phone)       //update a specified persons phone number;
    {
        ArrayList<Person> select=new ArrayList<Person>();

        for(Person a: addressb)
        {
            if(a.id.equals(id))
            {
                select.add(a);
                a.setPhone(phone);
            }   

        }

    }

    public boolean deletePerson(String id)          //delete a specified person;
    {
        Iterator<Person> it = addressb.iterator();
        boolean found = false;
        while(it.hasNext() && !found)
        {
            if(it.next().id.equals(id))
            {
                it.remove();
            }
        }
        return true;
    }
    
    public boolean checkPersonId(String id)         //check if the persons is in the system or not -- required for making a loan;
    {
        boolean found=false;
        for(Person a: addressb)
        {
            if(a.id.equals(id))
            {
                found=true;
            }
        }
        return found;
    }
}
